/*! \file
 * \brief Enter a brief description here.
 *
 * Enter a detailed description here. (optional)
 * \author Author (Ivo Wingelaar)
 * \date Date (2011)
 * \version Version (0.0.1)
 * \copyright Copyright (GNU LGPL v3)
 */

#include "LynxFPSCounter.hpp"

namespace Lynx
{

FPSCounter::FPSCounter() :
    mFPS(60),
    mPrimitive(0),
    mStartTime(0),
    mFramesCounted(0),
    mPrimitivesCounted(0),
    mPrimitiveAverage(0),
    mPrimitiveTotal(0)
{
    //
}


//! returns current fps
int32_t FPSCounter::get_fps() const
{
    return mFPS;
}


//! returns current primitive count
uint32_t FPSCounter::get_primitive() const
{
    return mPrimitive;
}


//! returns average primitive count of last period
uint32_t FPSCounter::get_primitive_average() const
{
    return mPrimitiveAverage;
}


//! returns accumulated primitive count since start
uint32_t FPSCounter::get_primitive_total() const
{
    return mPrimitiveTotal;
}


//! to be called every frame
void FPSCounter::register_frame(uint32_t now, uint32_t primitivesDrawn)
{
    ++mFramesCounted;
    mPrimitiveTotal += primitivesDrawn;
    mPrimitivesCounted += primitivesDrawn;
    mPrimitive = primitivesDrawn;

    const uint32_t milliseconds = now - mStartTime;

    if(milliseconds >= 1500)
    {
        const float invMilli = Myth::Math::reciprocal((float) milliseconds);

        mFPS = Myth::Math::ceil((1000 * mFramesCounted) * invMilli);
        mPrimitiveAverage = Myth::Math::ceil((1000 * mPrimitivesCounted) * invMilli);

        mFramesCounted = 0;
        mPrimitivesCounted = 0;
        mStartTime = now;
    }
}

} // namespace Lynx
